package com.codemonkey.lq.test;

import cn.hutool.core.io.FilenameUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StringUtil;

public final class LinuxTool {

	public static void main(String[] args) {
		replace_2();
	}
	
	/**
	 * 生成  替换目录下所有文件中指定字符为另外指定字符的SED命令字符串
	 * @return 
	 */
	public static String sedCommand_replaceSpecialCharInAllFile(final String strSearch,
			final String strReplace, final String foldPath) {
		// sed -i "s/查找的字符串/进行替换的字符串/g" `grep -rl 查找的字符串 目录路径`

		String foldPathLinuxFormat = FilenameUtil
				.separatorsToUnix(FilenameUtil.normalize(foldPath)).replace(":", "");
		// 首尾加上 /
		foldPathLinuxFormat = "/" + foldPathLinuxFormat;
		foldPathLinuxFormat = foldPathLinuxFormat.endsWith("/") ? foldPathLinuxFormat
				: foldPathLinuxFormat + "/";
		return StringUtil.format("sed -i \"s/{}/{}/g\" `grep -rl {} {}`", strSearch, strReplace,
				strSearch, foldPathLinuxFormat);
	}

	static void replace_2() {
		String orgin = "com.kanq.base.cache";
		String dest = "com.kanq.zrzy.plateform.cache";
		String rootPath = "d/Projects/BDCDJ/BDCDJ_KM/src/main/java/com/kanq/zrzy/plateform/cache";

		Console.log(LinuxTool.sedCommand_replaceSpecialCharInAllFile(orgin, dest, rootPath));

	}
}